﻿using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using EFERP;
using HRM.DICTIONARY.DAL;
using System;
using System.Windows.Forms;

namespace HRM.DICTIONARY.UI
{
    public partial class xucDepartmentAdd : XtraForm
    {
        private EmployeeLogicEx _logicEx;
        private hrm_department _obj;
        private readonly ICommon<hrm_department> _exeDic;
        private readonly ICommon<hrm_branch> _exeDicBranch;
        private CloseOrNew _closeNew = CloseOrNew.Close;

        public xucDepartmentAdd(ICommon<hrm_department> exeDic)
        {
            InitializeComponent();
            this._exeDicBranch = new Common<hrm_branch>(StaticVariable.ERPEn);
            this._exeDic = exeDic;
            this._exeDic.myAction = Actions.Insert;
            this._obj = new hrm_department();
            SetDataIntoGridLookup();
            SetDataIntoUI();
        }

        public xucDepartmentAdd(hrm_department currObj, ICommon<hrm_department> exeDic)
        {
            InitializeComponent();
            this._exeDicBranch = new Common<hrm_branch>(StaticVariable.ERPEn);this._exeDic = exeDic;
            this._exeDic.myAction = Actions.Update;
            this._obj = currObj;
            SetDataIntoGridLookup();
            SetDataIntoUI();
        }

        private void xucDepartmentAdd_FormClosing(object sender, FormClosingEventArgs e)
        {
            TotalClose();
        }

        private void SetDataIntoUI()
        {
            if (_obj == null)
                return;
            switch (_exeDic.myAction){
                case Actions.Update:
                    txtMaPhongBan.Text = _obj.departmentcode;
                    txtTenPhongBan.Text = _obj.departmentname;
                    txtMoTa.Text = _obj.description;
                    cbbThang.Text = string.Format("{0:00,0}", _obj.factquantity);
                    cbbTong.Text = string.Format("{0:00,0}", _obj.quantity);
                    glkChiNhanh.EditValue = _obj.branchcode;
                    txtDienThoai.Text = _obj.phone;

                    btnLuuDong.Visible = true;
                    btnThoat.Visible = true;
                    btnLuuThem.Visible = false;
                    break;
                case Actions.Insert:
                    _obj = new hrm_department();
                    txtMaPhongBan.Text = _exeDic.NewID("BC");
                    txtTenPhongBan.Text = "";
                    txtMoTa.Text = "";
                    cbbThang.Text = "0";
                    cbbTong.Text = "0";
                    glkChiNhanh.EditValue = "";
                    txtDienThoai.Text = _obj.phone;

                    btnLuuDong.Visible = true;
                    btnThoat.Visible = true;
                    btnLuuThem.Visible = true;
                    break;
                case Actions.InsertMultiple:
                    _obj = new hrm_department();
                     txtMaPhongBan.Text = _exeDic.NewID("BC");
                    txtTenPhongBan.Text = "";
                    txtMoTa.Text = "";
                    cbbThang.Text = "0";
                    cbbTong.Text = "0";
                    glkChiNhanh.EditValue = "";
                    txtDienThoai.Text = _obj.phone;

                    btnLuuDong.Visible = true;
                    btnThoat.Visible = true;
                    btnLuuThem.Visible = true;
                    break;
            }
        }

        private bool SetUIIntoData()
        {
            if (txtMaPhongBan.Text == "" || txtTenPhongBan.Text == "")
            {
                MessageBox.Show(@"Vui lòng không để trống thông tin văn phòng");
               
                return false;
            }
            if (glkChiNhanh.EditValue.ToString() == "")
            {
                MessageBox.Show(@"Vui lòng chọn chi nhánh của phòng ban này.");
                glkChiNhanh.Select();
                return false;
            }
            if (int.Parse(cbbThang.EditValue.ToString()) <= 0)
            {
                MessageBox.Show(@"Vui lòng nhập số lượng chứa tối đa nhân viên của phòng ban này.");
                cbbThang.Select();
                return false;
            }

            _obj.departmentcode = txtMaPhongBan.Text;
            _obj.departmentname = txtTenPhongBan.Text;
            _obj.description = txtMoTa.Text;
            _obj.quantity = int.Parse(cbbTong.EditValue.ToString());
            _obj.factquantity = int.Parse(cbbThang.EditValue.ToString());
            _obj.phone = txtDienThoai.Text;
            _obj.branchcode = glkChiNhanh.EditValue.ToString();

            hrm_branch objBranch = _exeDicBranch.GetById(_obj.branchcode);
            bool kq=false;
            _logicEx = new EmployeeLogicEx(objBranch, _obj);
            switch (_exeDic.myAction)
            {
                case Actions.Insert:
                    kq = _logicEx.CheckQuantityInBranchWhenCreateDepartment();
                    break;
                case Actions.InsertMultiple:
                    kq = _logicEx.CheckQuantityInBranchWhenCreateDepartment();
                    break;
                case Actions.Update:
                    kq = _logicEx.CheckQuantityInBranchWhenUpdateDepartment();
                    break;
            }
            if (kq) return true;
            MessageBox.Show(_logicEx.ErrMsg);
            return false;
        }

         //Add them if we have to combine the other tables to get data
        private bool SetDataIntoGridLookup()
        {
            bool kq = SetDataSysBrandIntoGridLookup();
            if (kq) return true;
            XtraMessageBox.Show(_exeDicBranch.ErrMsg, "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            return false;
        }

        private bool SetDataSysBrandIntoGridLookup()
        {
            bool kq = _exeDicBranch.AddGridLookupEdit(glkChiNhanh, "branchname", "branchcode");
            if (!kq) return false;
            //if (glkChiNhanh.Properties.DataSource != null)
            //{
            //    glkChiNhanh.EditValue = @"CN000001";}
            return true;
        }

        private void TotalControl()
        {
            bool kq;
            switch (_exeDic.myAction)
            {
                case Actions.Insert:
                    kq = SetUIIntoData();
                    if (kq)
                    {
                        kq = _exeDic.InsertOnGridView(_obj);
                        if (kq)
                        {
                            this.DialogResult = DialogResult.OK;
                            _exeDic.myAction = Actions.Insert;
                            this.Close();
                        }
                        else
                        {
                            MessageBox.Show(@"Lỗi thêm dữ liệu vào hệ thống");
                        }
                    }
                    break;

                case Actions.Update:
                    kq = SetUIIntoData();
                    if (kq)
                    {
                        kq = _exeDic.UpdateOnGridView(_obj);
                        if (kq)
                        {
                            this.DialogResult = DialogResult.OK;
                            _exeDic.myAction = Actions.Update;
                            this.Close();
                        }
                        else
                        {
                            _exeDic.ValueOfGridViewRow = null;
                            MessageBox.Show(@"Lỗi cập nhật dữ liệu vào hệ thống");
                        }
                    }
                    break;
                case Actions.InsertMultiple:
                    kq = SetUIIntoData();
                    if (kq)
                    {
                        kq = _exeDic.InsertOnGridView(_obj);
                        if (kq)
                        {
                            _exeDic.myAction = Actions.InsertMultiple;
                            SetDataIntoUI();
                            this.DialogResult = DialogResult.OK;
                            if (_closeNew == CloseOrNew.Close)
                                this.Close();
                            else
                                _closeNew = CloseOrNew.New;
                        }
                        else
                        {
                            MessageBox.Show(@"Lỗi thêm dữ liệu vào hệ thống");
                        }
                    }
                    break;
            }
        }
        private void TotalClose()
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
        private void btnThoat_Click(object sender, EventArgs e)
        {
            TotalClose();
        }

        private void btnLuuDong_Click(object sender, EventArgs e)
        {
            _closeNew = CloseOrNew.Close;
            TotalControl();
        }

        private void btnLuuThem_Click(object sender, EventArgs e)
        {
            _exeDic.myAction = Actions.InsertMultiple;
            _closeNew = CloseOrNew.New;
            TotalControl();
        }

        private void glkChiNhanh_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {if (e.Button.Kind != ButtonPredefines.Plus) return; xucBranchAdd add = new xucBranchAdd(_exeDicBranch);
            add.ShowDialog();
        }
    }
}
